import os

from openai import OpenAI

# 'http://localhost:11434/v1/'
client = OpenAI(
    base_url=os.getenv("OPENAI_API_URL", 'http://localhost:11434/v1/'),
    api_key='ollama',  # required but ignored
)


# q = """
# "['姓名陈福海', '性别男', '民旅图', '出生1986年9月12日', '址甘肃省平凉市岭区大秦', '回族乡梁西村一社47号', '公民身份号码622701198609122613'] " + "请给出正确的身份证信息json,包含:姓名、性别、民族、出生、身份证号、住址，身份证号必须是完整的18位数字,输出的格式为 {type:ID,name:xxx,sex:xxx,nation:xxx ,birth:xxx,idNo:xxxxxxxxxxxxxxxxx ,address:xxx}" 只输出json格式数据，不要输出其他内容
# """
# chat_completion = client.chat.completions.create(
#     messages=[
#         {
#             'role': 'user',
#             'content': q,
#         }
#     ],
#     model='qwen2.5:14b',
# )
#
# # 提取 content
# content = chat_completion.choices[0].message.content
# content = content.strip()
# print(content)


def qwen_completion(content, question):
    """
    调用qwen2.5模型
    :param content:
    :param question:
    :param model:
    """
    model = os.getenv("OPENAI_API_MODEL", 'qwen2.5:7b')

    chat_completion = client.chat.completions.create(
        messages=[
            {
                'role': 'user',
                'content': f"{content}\n\nQuestion: {question}",
            }
        ],
        model=model,
    )

    # 提取 content
    completion_content = chat_completion.choices[0].message.content
    strip_content = completion_content.strip()

    return strip_content
